package com.hongxinguoji.manager.job.service.impl;

import java.util.Date;
import java.util.List;

import javax.print.attribute.standard.JobOriginatingUserName;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hongxinguoji.manager.common.util.EasyUIDataGridResult;
import com.hongxinguoji.manager.common.util.HXResult;
import com.hongxinguoji.manager.job.mapper.HxJobMapper;
import com.hongxinguoji.manager.job.pojo.HxJob;
import com.hongxinguoji.manager.job.pojo.HxJobExample;
import com.hongxinguoji.manager.job.service.IJobService;

@Service
public class JobServiceImpl implements IJobService {

	@Autowired
	private HxJobMapper JobMapper;

	@Override
	public HXResult add(HxJob Job, String desc) {

		Job.setStatus(1);
		// 向表插入数据
		JobMapper.insert(Job);

		// 返回状态码为200，发布成功。
		return HXResult.ok();
	}

	@Override
	public EasyUIDataGridResult getList(Integer page, Integer rows) {
		// 查询新闻列表
		// 分页处理
		PageHelper.startPage(page, rows);

		HxJobExample example = new HxJobExample();
		List<HxJob> list = JobMapper.selectByExample(example);

		PageInfo<HxJob> pageInfo = new PageInfo<>(list);

		EasyUIDataGridResult result = new EasyUIDataGridResult();

		result.setTotal(pageInfo.getTotal());
		result.setRows(list);

		return result;
	}

	@Override
	public HXResult deleteOrRestore(String ids, int flag) {
		// 截取字符串
		String[] idStrings = ids.split(",");
		// 遍历ID，进行逻辑删除操作
		for (String id : idStrings) {
			String tID = id.trim();
			// 转换id类型
			// Long i = Long.parseLong(tID);
			Integer i = Integer.parseInt(tID);
			HxJob hxJob = JobMapper.selectByPrimaryKey(i);
			// 逻辑删除操作（1，正常，0，删除）
			hxJob.setStatus(flag);
			// 更新操作
			JobMapper.updateByPrimaryKey(hxJob);
		}
		// 返回修改成功，状态码200
		return HXResult.ok();
	}

	@Override
	public void update(HxJob job) {
		Integer id = job.getId();

		HxJob hxJob = JobMapper.selectByPrimaryKey(id);

		// 更新数据操作

		// 分类
		hxJob.setJobCategory(job.getJobCategory());
		// 招聘岗位名称
		hxJob.setJobName(job.getJobName());
		// 男女
		hxJob.setSex(job.getSex());
		// 学历
		hxJob.setEducation(job.getEducation());
		// 数量
		hxJob.setJobNum(job.getJobNum());
		// 是否发布
		hxJob.setIspush(job.getIspush());

		// 更新薪资
		hxJob.setRemark(job.getRemark());

		// 更新日期
		if (job.getIspush() == 1) {
			hxJob.setJobPushtime(new Date());
		}

		if (StringUtils.isNoneBlank(job.getContent())) {
			hxJob.setContent(job.getContent());
		}

		// 更新
		JobMapper.updateByPrimaryKey(hxJob);

	}

}
